<template>
  <el-dialog :title="showTitile" :visible="showDialog" @close="close">
    <!--    放置弹层内容-->
    <el-form label-width="120px" :model="depForm" :rules="depRules" ref="depForm">
      <el-form-item label="部门名称" prop="name">
        <el-input placeholder="2-10个字符" v-model="depForm.name" size="mini" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input placeholder="2-10个字符" v-model="depForm.code" size="mini" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item label="部门负责人" prop="managerId">
        <el-select placeholder="请选择负责人" v-model="depForm.managerId" size="mini" style="width: 80%">
          <!--  label:显示下拉选项的内容  value是选中的内容，存到v-model上        -->
          <el-option
            v-for="item in manageList"
            :key="item.id"
            :label="item.username"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input placeholder="1-100个字符" type="textarea" v-model="depForm.introduce" size="mini" style="width: 80%"></el-input>
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button type="primary" @click="btnOk" size="mini">确定</el-button>
            <el-button size="mini" @click="btnCancel">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import {getDepartmentList, getManageList, addDepartment, getDepartDetail, editDepartDetail} from '@/api/department'

export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    currentNodeId: {
      type: Number,
      default: null
    }
  },
  data() {
    return {
      depForm: {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      },
      depRules: {
        code: [
          {
            required: true,
            message: '部门编码不能为空',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            min: 2,
            max: 10,
            message: '部门编码2-10字符'
          },
          {
            trigger: 'blur',
            validator: async (rule, value, callback) => {
              let res = await getDepartmentList()
              if (this.depForm.id) {
                // 编辑 排除自身
                res = res.filter(item => item.id !== this.depForm.id)
              }
              if (res.some(item => item.code === value)) {
                callback(new Error('部门中已经有该编码了'))
              } else {
                callback()
              }
            }
          }
        ],
        name: [
          {
            required: true,
            message: '部门名称不能为空',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            min: 2,
            max: 10,
            message: '部门名称2-10字符'
          },
          {
            trigger: 'blur',
            validator: async (rule, value, callback) => {
              let res = await getDepartmentList()
              if (this.depForm.id) {
                // 编辑 排除自身
                res = res.filter(item => item.id !== this.depForm.id)
              }
              if (res.some(item => item.name === value)) {
                callback(new Error('部门中已经有该名称了'))
              } else {
                callback()
              }
            }
          }
        ],
        introduce: [
          {
            required: true,
            message: '部门介绍不能为空',
            trigger: 'blur'
          },
          {
            trigger: 'blur',
            min: 1,
            max: 100,
            message: '部门介绍1-100字符'
          }
        ],
        managerId: [
          {
            required: true,
            message: '部门负责人不能为空',
            trigger: 'change'
          }
        ]
      },
      manageList: []
    }
  },
  computed: {
    showTitile() {
      return this.depForm.id ? '编辑部门' : '新增部门'
    }
  },
  created() {
    this.getManageList()
  },
  methods: {
    async getManageList() {
      const res = await getManageList()
      this.manageList = res
    },
    close() {
      // resetFields 只能重置模板中绑定的数据 id清不掉
      this.depForm = {
        code: '',
        introduce: '',
        managerId: '',
        name: '',
        pid: ''
      }
      this.$refs.depForm.resetFields()
      // 修改父组件的值
      this.$emit('update:showDialog', false)
    },
    // 点击确定
    btnOk() {
      this.$refs.depForm.validate(async isOk => {
        if (isOk) {
          let msg = '新增'
          if (this.depForm.id) {
            msg = '编辑'
            // 编辑 this.depForm 包含id （因为如果没有，是进不来这个里面的，就是靠id区分的）
            await editDepartDetail(this.depForm)
          } else {
            // 新增
            await addDepartment({
              ...this.depForm,
              pid: this.currentNodeId
            })
          }
          // 通知父组件更新
          this.$emit('updateDepartment')
          this.$message.success(`${msg}部门成功`)
          this.close()
        }
      })
    },
    async getDepartDetail() {
      const res = await getDepartDetail(this.currentNodeId)
      this.depForm = res
    },
    btnCancel() {
    }
  }
}
</script>

<style scoped>

</style>
